package com.se.xb.service;

import com.se.xb.dao.ArticleDao;
import com.se.xb.entity.Article;
import com.se.xb.entity.dto.ArticleDto;
import com.se.xb.entity.dto.PageResult;
import com.se.xb.entity.dto.SearchDto;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ArticleService {
    ArticleDao articleDao = new ArticleDao();

    public PageResult<Article> getArticleList(SearchDto<String> searchDto) {
        ArrayList<String> queryList = searchDto.getQueryList();
        String userId = queryList.get(0);
        String title = queryList.get(1);
        Integer currentPage = searchDto.getCurrentPage();
        List<Article> articleList = articleDao.getArticleList(currentPage, userId, title);
        Integer articleCount = articleDao.getArticleCount(userId, title);
        PageResult<Article> articlePageResult = new PageResult<>();
        articlePageResult.setRecords(articleList);
        articlePageResult.setPage(currentPage);
        articlePageResult.setTotal(articleCount);
        if (articlePageResult.getTotal() % 3 == 0) {
            articlePageResult.setTotalPage(articlePageResult.getTotal() / 3);
        } else {
            articlePageResult.setTotalPage(articlePageResult.getTotal() / 3 + 1);
        }
        return articlePageResult;
    }

    public PageResult<Article> getFavoriteList(SearchDto<String> searchDto) {
        ArrayList<String> queryList = searchDto.getQueryList();
        String userId = queryList.get(0);
        String title = queryList.get(1);
        Integer currentPage = searchDto.getCurrentPage();
        List<Article> articleList = articleDao.getFavoriteList(currentPage, userId, title);
        Integer articleCount = articleDao.getFavoriteArticleCount(Long.parseLong(userId),title);
        PageResult<Article> articlePageResult = new PageResult<>();
        articlePageResult.setRecords(articleList);
        articlePageResult.setPage(currentPage);
        articlePageResult.setTotal(articleCount);
        if (articlePageResult.getTotal() % 3 == 0) {
            articlePageResult.setTotalPage(articlePageResult.getTotal() / 3);
        } else {
            articlePageResult.setTotalPage(articlePageResult.getTotal() / 3 + 1);
        }
        return articlePageResult;
    }

    public List<ArticleDto> getFavoriteList(List<Article> articleList, Long userId) {
        Map<String, Integer> favoriteLikes = articleDao.getFavoriteLikes(userId);
        ArrayList<ArticleDto> articleDtos = new ArrayList<>();
        for (Article article : articleList) {
            ArticleDto articleDto = new ArticleDto();
            articleDto.setId(article.getId());
            articleDto.setTitle(article.getTitle());
            articleDto.setContent(article.getContent());
            articleDto.setBrowseCount(article.getBrowseCount());
            articleDto.setPublishDate(article.getPublishDate());
            articleDto.setPublishRealName(article.getPublishRealName());
            articleDto.setUserId(article.getUserId());
            if (favoriteLikes.containsKey(article.getId().toString())) {
                articleDto.setFavoriteCount(favoriteLikes.get(article.getId().toString()));
            }
            articleDtos.add(articleDto);
        }
        return articleDtos;
    }


    public List<Article> getFavoriteList(Long userId) {
        return articleDao.getFavoriteList(userId);
    }

    public ArticleDto getFavorite(Article article) {
        ArticleDto articleDto = new ArticleDto();
        Long id = article.getId();
        articleDto.setId(article.getId());
        articleDto.setTitle(article.getTitle());
        articleDto.setContent(article.getContent());
        articleDto.setBrowseCount(article.getBrowseCount());
        articleDto.setPublishDate(article.getPublishDate());
        articleDto.setPublishRealName(article.getPublishRealName());
        articleDto.setUserId(article.getUserId());
        Integer favoriteCount = articleDao.getFavoriteCount(id);
        articleDto.setFavoriteCount(favoriteCount);
        return articleDto;
    }

    public Article getArticle(Long id) {
        return articleDao.getArticle(id);
    }


    public void updateArticle(Article article) throws SQLException {
        articleDao.updateArticle(article);
    }

    public void addArticle(Article article) throws SQLException {
        articleDao.addArticle(article);
    }

    public void favorite(Long userId, Long articleId) throws SQLException {
        articleDao.favorite(userId, articleId);
    }

    public void unfavorite(Long userId, Long articleId) throws SQLException {
        articleDao.unfavorite(userId, articleId);
    }
}
